package com.cjbdi.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

import sun.misc.BASE64Encoder;

public class NumUtil {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println(NumUtil.GetDate("2018-12-12", "yyyy年M月d日"));
		//System.out.println(getLastMonthFinalDate());
		System.out.println(System.getenv("MPJ_HOME"));
	}
	public static boolean isNumeric(String s){//是否为整数
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(s).matches();
	}
	public static String getMonthNum(String month) {
		month = month.replace("月", "");
		if(Integer.parseInt(month)<=10) {
			month = "0"+month;
		}
		return month;
	}
	/**
     * 
     * 描述:获取上个月的最后一天.
     * 
     * @return
     */
    public static String getLastMonthFinalDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }
    /**
     * 
     * 描述:获取去年上个月的最后一天.
     * 
     * @return
     */
    public static String getLastYearLastMonthFinalDate(String format) {
        SimpleDateFormat dft = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, -1);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }
	public static String toWan(int number) {
		if(number>=1000) {
			double num = (double) number / 10000;// 将数字转换成以万为单位的数
			BigDecimal b = new BigDecimal(num);
			double d = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
			String jian = (d+"万");
			return jian.replace(".0", "");			
		}/*else {
			if(type.equals("位")) {
				return number+"位";
			}else if(type.equals("件")||type.equals("人")){
				return number+"";
			}else {
				return "";
			}
		}*/
		else if(number>=100 && number<1000) {
			int num = number / 100;
			return num+"00余";
		}else if(number>0  && number<100) {
			//return "数十";
			return number+"";
		}else {
			return "0";
		}
		
	}
	
	/*public static double toWan(String number,String type) {
		double num = Double.parseDouble(number) / 10000;// 将数字转换成以万为单位的数
		BigDecimal b = new BigDecimal(num);
		double d = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		return d;
	}*/

	public static double toNxiaoshu(double number, int n) {
		NumberFormat nf = new DecimalFormat("#");
		double d = new BigDecimal(number).setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
		return d;
	}
	
	public static String toNxiaoshuReturnStr(double number, int n) {
		double d = new BigDecimal(number).setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
		if(d % 1.0 == 0){
			return String.valueOf((long)d);
		}
		return String.valueOf(d);
	}

	public static double toNxiaoshu(String number, int n) {
		if(number.equals("")||number.equals("--")) {
			return 0;
		}
		double d = new BigDecimal(number).setScale(n, BigDecimal.ROUND_HALF_UP).doubleValue();
		return d;
	}

	public static String toSheng(String fymc) {
		if (fymc.indexOf("黑龙江") != -1 || fymc.indexOf("内蒙古") != -1) {
			return fymc.substring(0, 3);
		} else {
			return fymc.substring(0, 2);
		}
	}

	public static String getImageStr(String imgFile) {
		// String imgFile = "d:/1.png";
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}
	public static String getZyImageStr(String tempFile,String imgFile) {
		// String imgFile = "d:/1.png";
		File file = new File(imgFile);
		if(!file.exists()) {
			imgFile = tempFile;
		}
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream(imgFile);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);
	}
	public static String GetNowDate(String format){   
	    String temp_str="";   
	    Date dt = new Date();   
	    //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制   
	    SimpleDateFormat sdf = new SimpleDateFormat(format);   
	    temp_str=sdf.format(dt);   
	    return temp_str;   
	}
	public static String GetDate(String date , String format){       
	    //最后的aa表示“上午”或“下午”    HH表示24小时制    如果换成hh表示12小时制     
	    Date dt = null;
		try {
			dt = new SimpleDateFormat("yyyy-MM-dd").parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	    return new SimpleDateFormat(format).format(dt);   
	}
	public static String toChineseNum(int num) {
		if(num>9) {
			return num+"";
		}else {
			String[] s = {"一", "二", "三", "四", "五", "六", "七", "八", "九" };
	        String result = "--";
	        for (int i = 1; i <= 9; i++) {
	        	if(i == num) {
	        		result = s[i-1];
	        		break;
	        	}
	        }
	        return result;
		} 
    }
	public static String toChineseNum(String num) {
		if(num.indexOf("=")!=-1) {
			num = num.substring(num.indexOf("=")+1,num.length()-1);
		}
		if(Integer.parseInt(num)>9) {
			return num;
		}else {
			String[] s = {"一", "二", "三", "四", "五", "六", "七", "八", "九" };
	        String result = "0";
	        for (int i = 1; i <= 9; i++) {
	        	if(i == Integer.parseInt(num)) {
	        		result = s[i-1];
	        		break;
	        	}
	        }
	        return result;
		}
        
    }
	public static String getPastDate(String date,String format) {  
		if (date.length()>4) {//为超审限的日期2018-10-28
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			Date dt = null;
			try {
				dt = sdf.parse(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(dt);
			rightNow.add(Calendar.YEAR, -1);
			Date dt1 = rightNow.getTime();
			String reStr = sdf.format(dt1);
			return reStr;
		} else {
			if(format.length()>4) {
				return (Integer.parseInt(date) - 1) + "";
			}else {
				return date;
			}
		}
		
    }  
	public static String getHalfYearReportDate(String date, String type) {  
		if(type.equals("year")||type.equals("up")) {
			return (Integer.parseInt(date) - 1) + "";
		}else {
			return date;
		}
	}
	public static String getHalfYearReportType(String type) {  
		if(type.equals("year")) {
			return type;
		}else if(type.equals("up")) {
			return "down";
		}else {
			return "up";
		}
	}

}
